extends IUsableComponent


class_name ComplexComponent
"""
多个EquipableComponent组合的效果
"""


## 是否忽略错误
@export var is_ignore_error: bool = true
## 多个效果
@export var elements: Array[IUsableComponent] = []


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "elements": IComponent.find_children.bind(self, func(x): return x is IUsableComponent, elements),
    }, super.__check_exports())

## 应用效果
func apply(character: CharacterComponent) -> bool:
    if not super.apply(character):
        return false
    var succ_elements: Array[IUsableComponent] = []
    var is_error: bool = false
    for e in elements:
        if e.apply(character):
            succ_elements.append(e)
        elif not is_ignore_error:
            is_error = true
            break
    if is_error:
        succ_elements.reverse()
        for e in succ_elements:
            e.cancel()
        super.cancel()
        return false
    return true

## 取消效果
func cancel() -> void:
    var count: int = elements.size()
    for i in range(count):
        var e: IUsableComponent = elements[count - i - 1]
        if e.is_used():
            e.cancel()
    super.cancel()
